#!/usr/bin/perl

use ApacheConfig;

package ApacheConfigFile;

sub new {
	my $class = shift;

	my $self = {
		_filename => shift,
		_apacheconfig => new ApacheConfig(),
	};

	bless $self,$class;
	return $self;
}

sub addDirectory {
	my ($self,$directory ) = @_;
	my $apconfig=\$self->{_apacheconfig};
	$$apconfig->addDirectoryName($directory);
}

sub addModule {
	my ($self, $mod) = @_;
	my $apconfig=\$self->{_apacheconfig};
	$$apconfig->addModule($mod);
}

sub addInclude {
	my ($self, $include) = @_;
	my $apconfig=\$self->{_apacheconfig};
	$$apconfig->addInclude($include);
}

sub addIncludeFile {
	my ($self, $include) = @_;
	my $apconfig=\$self->{_apacheconfig};
	$$apconfig->addIncludeFile($include);
}

sub getDirectory {
	my ($self,$id) = @_;
	return ${$self->{_directories}}{$cdid};
}

sub getDirectories {
	my ($self )= @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getDirectories;
}

sub getErrorDocumentValue {
	my ($self,$code)=@_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getErrorDocumentValue($code);
}

sub getIncludes {
	my ($self )= @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getIncludes;
}

sub getIncludeFiles {
	my ($self )= @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getIncludeFiles;
}

sub getAliases {
	my ($self )= @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getAliases;
}

sub getAliasesDirs {
	my ($self )= @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getAliasesDirs;
}

sub getScriptAliases {
	my ($self )= @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getScriptAliases;
}

sub getScriptAliasesDirs {
	my ($self )= @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getScriptAliasesDirs;
}

sub getModules {
	my ($self )= @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getModules;
}

sub setConfigFile {
	my ( $self , $filename) = @_;
	$self->{_filename} = $filename if defined($filename);
}

sub setConfiguration {
	my ( $self , @conf_lines ) = @_;
	$self->{_conf_lines} = [];
	@{$self->{_conf_lines}} = @conf_lines;
}

sub setDirective {
	my ($self, $key, $value) = @_;
	my $apconfig=\$self->{_apacheconfig};
	$$apconfig->setDirective($key,$value);
}

sub setAlias{
	my ($self, $key, $value) = @_;
	my $apconfig=\$self->{_apacheconfig};
	$$apconfig->setAlias($key,$value);
}

sub setScriptAlias{
	my ($self, $key, $value) = @_;
	my $apconfig=\$self->{_apacheconfig};
	$$apconfig->setScriptAlias($key,$value);
}

sub setErrorDocument{
	my ($self, $code, $response) = @_;
	my $apconfig=\$self->{_apacheconfig};
	$$apconfig->setErrorDocument($code,$response);
}

sub getDirective {
	my ($self,$key) = @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getDirective($key);
}

sub getErrorDocument {
	my ($self,$key) = @_;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getErrorDocument();
}

sub getConfigFile {
	my ( $self ) = @_;
	return $self->{_filename};
}

sub getConfiguration {
	my ( $self ) = @_;
	return @{$self->{_conf_lines}};
}

sub readConfigFile {
	my ( $self ) = shift;
	open APACHE_CONF, $self->getConfigFile;
	$self->setConfiguration(<APACHE_CONF>);
	close APACHE_CONF;
}

sub getIncludes {
	my ($self) = shift;
	my $apconfig=\$self->{_apacheconfig};
	return $$apconfig->getIncludes();
	
}

sub readIncludes {
	my ($self) = shift;
	my $serverroot=$self->getDirective('ServerRoot');
	if ( $serverroot =~ /\"(.*)\"/){
		$serverroot=$1;
	}
	my @includes=$self->getIncludes;
	my @files=();
	foreach (@includes){
		my $includeexp=$_;
		my $realpath;
		my @newfiles=();
		print $includeexp ."\n";
		if($includeexp !~ /^\//){
			if($serverroot =~ /\/$/){
				$realpath=$serverroot . $includeexp;
			}else{

				$realpath=$serverroot ."/". $includeexp;
			}
			@newfiles=`ls $realpath`;
			foreach (@newfiles){
				chomp;
				$self->addIncludeFile($_);	
			}
		}else{
		}
	}
}

sub readBasicConfiguration {
	my ( $self ) = shift;
	my @lines = ();
	foreach ($self->getConfiguration()){
		chomp;
		if( $_ !~ /^\s*#/ ){
			push(@lines,$_);
			if ( $_ =~ /(ServerTokens)\s+(\w*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /(ServerRoot)\s+([\w|\/|\"]*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /(LoadModule)\s+([\w|\.]*)\s*/ ){
				$self->addModule($2);
			}
			if ( $_ =~ /(User)\s+(\w*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /(Group)\s+([\w|\/]*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /(DocumentRoot)\s+([\w|\/|\"]*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /(Listen)\s+([\d|,]*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /^\s*<(Directory)\s+([\w|\/|\"|-]*)\s*/ ){
				$self->addDirectory($2);
			}
			if ( $_ =~ /^\s*(Include)\s+([\w|\/|\"|\-|\.|\*]*)\s*/ ){
				$self->addInclude($2);
			}
			if ( $_ =~ /^\s*(ErrorLog)\s+([\w|\/|\"|\-|\.|\*]*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /^\s*(LogLevel)\s+([\w|\/|\"|\-|\.|\*]*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /^\s*(ServerSignature)\s+([\w|\/|\"|\-|\.|\*]*)\s*/ ){
				$self->setDirective($1,$2);
			}
			if ( $_ =~ /^\s*(Alias)\s+([\w|\/|\"|\-|\.|\*]*)\s+([\w|\/|\"|-|\*]*)/ ){
				$self->setAlias($2,$3);
			}
			if ( $_ =~ /^\s*(ScriptAlias)\s+([\w|\-|\/|\.]+)\s+([\w|\-|\/|\.|\"]+)/){
				$self->setScriptAlias($2,$3);
			}
			if ( $_ =~ /^\s*(ErrorDocument)\s+(\d{3})\s+(.*)$/) {
				$self->setErrorDocument($2,$3);
			}
		}
	}

}

1;
